package com.magic.smartcity.view.more.adapters

import android.app.Application
import android.content.res.Resources
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.databinding.DataBindingUtil
import androidx.navigation.Navigation
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.Resource
import com.magic.smartcity.R
import com.magic.smartcity.base.BaseApplication
import com.magic.smartcity.beans.HospitalDetail
import com.magic.smartcity.databinding.ItemHospitalHomeListBinding
import com.magic.smartcity.utils.Constant

/**
 * @description:
 * @create: Create by Magic on 2022/2/19.
 */
class HospitalHomeListAdapter : RecyclerView.Adapter<HospitalHomeListAdapter.ItemHolder>() {
    val list: MutableList<HospitalDetail> = mutableListOf()

    class ItemHolder(binding: ItemHospitalHomeListBinding) : RecyclerView.ViewHolder(binding.root) {
        val binding = binding
    }

    fun setData(data: List<HospitalDetail>) {
        list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }

    private val TAG: String = "HospitalHomeListAdapter"
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        val inflate = DataBindingUtil.inflate<ItemHospitalHomeListBinding>(
            LayoutInflater.from(parent.context),
            R.layout.item_hospital_home_list,
            parent,
            false
        )
        return ItemHolder(inflate)
    }

    override fun onBindViewHolder(holder: ItemHolder, position: Int) {
        holder.binding.hospitalDetail = list[position]
        Glide.with(holder.binding.imageView4).load(Constant.Service_IP + list[position].imgUrl)
            .error(R.color.gray).into(holder.binding.imageView4)
        for (i in 1..list[position].level) {
            val imageView = ImageView(holder.binding.root.context)
            imageView.setImageDrawable(holder.binding.root.context.getDrawable(R.drawable.ic_star))
            holder.binding.starLeve.addView(imageView)
        }
        holder.binding.root.setOnClickListener{
            var bundle = Bundle()
            bundle.putInt("id",list[position].id)
            Navigation.findNavController(it).navigate(R.id.action_hospitalFragment_to_hospitalDetailFragment,bundle)
        }
    }

    override fun getItemCount() = list.size
}